package com.fastwire.carriere.hibernate.daoimpl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.common.constant.OrderConstants;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.common.util.DateUtil;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.dao.OrderDao;
import com.fastwire.carriere.serviceorder.entity.ActivityStatus;
import com.fastwire.carriere.serviceorder.entity.ServiceOrder;
import com.fastwire.carriere.serviceorder.vo.OrderSearchCriteriaVo;

// TODO: Auto-generated Javadoc
/**
 * The Class OrderDaoImpl.
 */
public class OrderDaoImpl extends AbstractDAOImpl implements OrderDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.OrderDao#findAllOrders(java.util.Date,
	 * java.util.Date)
	 */
	@Override
	public List<ServiceOrder> findServiceOrdersbySearchCriteria(
			OrderSearchCriteriaVo searchCriteriaVo) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(ServiceOrder.class);

		if (!StringUtil.isEmptyString(searchCriteriaVo.getCustomerName())) {
			criteria.add(getWildCardCriterion("customerName",
					searchCriteriaVo.getCustomerName()));
		}

		if (!StringUtil.isEmptyString(searchCriteriaVo.getOrderNo())) {
			criteria.add(getWildCardCriterion("workOrderNo",
					searchCriteriaVo.getOrderNo()));
		}

		// modified by Jun on 20120321
		if (!StringUtil.isEmptyString(searchCriteriaVo.getOrderCreateStartDate()) 
		        || !StringUtil.isEmptyString(searchCriteriaVo.getOrderCreateEndDate()) ) {
			/*
		    criteria.add(getDateCompareCriterion(
			        "orderCreateDate", //"customerReqDate", 
					DateUtil.parseDate(DateUtil.DATE_SLASH,
							searchCriteriaVo.getOrderCreateStartDate()),
					DateUtil.parseDate(DateUtil.DATE_SLASH,
							searchCriteriaVo.getOrderCreateEndDate())));
			*/
			getDateCompareCriterion(criteria,
                    "orderCreateDate", //"customerReqDate",  
                    DateUtil.parseDate(DateUtil.DATE_SLASH,
                            searchCriteriaVo.getOrderCreateStartDate()),
                    DateUtil.parseDate(DateUtil.DATE_SLASH,
                            searchCriteriaVo.getOrderCreateEndDate()));
		}
		
        // modified by Jun on 20120321
		if (!StringUtil.isEmptyString(searchCriteriaVo.getCustReqStartDate()) 
		        || !StringUtil.isEmptyString(searchCriteriaVo.getCustReqEndDate()) ) {
		    /*
			criteria.add(getDateCompareCriterion(
					"customerReqDate",
					DateUtil.parseDate(DateUtil.DATE_SLASH,
							searchCriteriaVo.getCustReqStartDate()),
					DateUtil.parseDate(DateUtil.DATE_SLASH,
							searchCriteriaVo.getCustReqEndDate())));
							
		    */
		    getDateCompareCriterion(criteria, 
                    "customerReqDate",
                    DateUtil.parseDate(DateUtil.DATE_SLASH,
                            searchCriteriaVo.getCustReqStartDate()),
                    DateUtil.parseDate(DateUtil.DATE_SLASH,
                            searchCriteriaVo.getCustReqEndDate()));
		}

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	@Override
	public ServiceOrder getServiceOrdersbyWorkOrderNo(String workOrderNo)
			throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(ServiceOrder.class);
		criteria.add(Restrictions.eq("workOrderNo", workOrderNo));
		return (ServiceOrder) getHibernateTemplate().findSingleObjByCriteria(
				criteria, false, true);

	}

	@Override
	public List<ActivityStatus> getWIPOrderActivitiesbyWorkOrderNo(
			String workOrderNo) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(ActivityStatus.class);
		criteria.add(Restrictions.eq("workOrderNo", workOrderNo));
		criteria.add(Restrictions.eq("statusCd",
				OrderConstants.ACTIVITY_STATUS_WIP));
		return (List<ActivityStatus>) getHibernateTemplate()
				.findSingleObjByCriteria(criteria, false, false);

	}

	@Override
	public List<ServiceOrder> getServiceOrdersWithDueDate(Date dueDate)
			throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(ServiceOrder.class);

		criteria.add(Restrictions.eq("customerReqDate",
				DateUtil.truncateDate(dueDate)));
		criteria.add(Restrictions.ne("orderActionType", "cancelOrder"));
		/*
		 * criteria.setFetchMode("activities", FetchMode.JOIN)
		 * .createAlias("activities", "activities")
		 * .add(Restrictions.in("activities.activityName",
		 * OrderConstants.CEBS_ACTIVITIES))
		 * .add(Restrictions.eq("activities.activityStatus",
		 * OrderConstants.ACTIVITY_STATUS_RDY))
		 * .add(Restrictions.like("activities.groupId",
		 * OrderConstants.GROUP_ID_GSP, MatchMode.START));
		 */

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	@Override
	public List<ActivityStatus> getActivityStatusbyWorkOrders(
			List<String> orderIdList) throws DAOException {
		
		// added by Jun on 20120419
		if(null == orderIdList || 0 >= orderIdList.size()) {			
			CommonLogger.info(this, "getActivityStatusbyWorkOrders: no order found.");
			return null;
		}
		
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ActivityStatus.class);
		criteria.add(Restrictions.in("workOrderNo", orderIdList));

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}
}
